﻿using AMatysiak.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using AMatysiak.Methods;

namespace AMatysiakTest.Reflection
{
    [TestClass]
    public class StringSerializerTest
    {
        [Serializable]
        private class SomeClass
        {
            public int A { get; set; }
            public string B { get; set; }
            public double[] C { get; set; }
            public SomeClass D { get; set; }

            public override bool Equals(object obj)
            {
                if (obj is SomeClass)
                {
                    SomeClass obj2 = (SomeClass)obj;
                    if (A == obj2.A && B == obj2.B && C.Equality(obj2.C) && (D == null || D.Equals(obj2.D)))
                        return true;
                    return false;
                }

                return base.Equals(obj);
            }

            public override int GetHashCode()
            {
                return base.GetHashCode();
            }
        }

        private SomeClass sc = new SomeClass()
                    {
                        A = 1,
                        B = "2",
                        C = new double[] { 3, 4, 5 },
                        D = new SomeClass()
                        {
                            A = 6,
                            B = "7",
                            C = new double[] { 8.0, 9.5 },
                            D = null
                        }
                    };

        private string proper = "AAEAAAD/////AQAAAAAAAAAMAgAAAERBTWF0eXNpYWtUZXN0LCBWZXJzaW9uPTEuMC4wLjAsIEN1bHR1cmU9bmV1dHJhbCwgUHVibGljS2V5VG9rZW49bnVsbAUBAAAAN0FNYXR5c2lha1Rlc3QuUmVmbGVjdGlvbi5TdHJpbmdTZXJpYWxpemVyVGVzdCtTb21lQ2xhc3MEAAAAEjxBPmtfX0JhY2tpbmdGaWVsZBI8Qj5rX19CYWNraW5nRmllbGQSPEM+a19fQmFja2luZ0ZpZWxkEjxEPmtfX0JhY2tpbmdGaWVsZAABBwQIBjdBTWF0eXNpYWtUZXN0LlJlZmxlY3Rpb24uU3RyaW5nU2VyaWFsaXplclRlc3QrU29tZUNsYXNzAgAAAAIAAAABAAAABgMAAAABMgkEAAAACQUAAAAPBAAAAAMAAAAGAAAAAAAACEAAAAAAAAAQQAAAAAAAABRAAQUAAAABAAAABgAAAAYGAAAAATcJBwAAAAoPBwAAAAIAAAAGAAAAAAAAIEAAAAAAAAAjQAs=";
           
        [TestMethod]
        public void SerializeTest()
        {
            string serial = StringSerializer.Serialize(sc);
            Assert.AreEqual(proper, serial);
        }

        [TestMethod]
        public void DeserializeTest()
        {
            object serial = StringSerializer.Deserialize(proper);
            SomeClass sc2 = (SomeClass)serial;
            Assert.AreEqual(sc, sc2);
        }
    }
}
